// pages/search/search.js
const app = getApp()
const util = require('../../utils/util.js')
const time = require('../../utils/time.js')
Page({
        data: {
                isopen: 0, //未搜索
                pages: 1, //默认首页
                searchtext: '', //input值
                list: []
        },
        onLoad: function(options) {
                var that = this
                that.setData({
                        headerheight: app.globalData.navHeight
                })
                that.hotreq()
                that.read()
        },
        //   获取搜词
        inputing: function(e) {
                this.setData({
                        searchtext: e.detail.value
                })
        },
        // 取消
        cancel: function() {
                if(this.data.searchtext==""){
                        wx.reLaunch({
                                url: '/pages/index/index',
                        })
                }else{
                        this.setData({
                                searchtext: '',
                                pages: 1,
                                isopen: 0,
                                show1: false,
                                show2: false,
                        })
                        this.read()
                }
        },
        // 清除
        quanclcer: function() {
                var that = this
                wx.setStorageSync('set', '') //存
                that.read()
        },
        //   键盘搜索
        searchBtn: function() {
                var that = this
                that.setData({
                        pages: 1
                })
                that.request()
                that.Storage()
                that.read()
        },
        //    单独删除历史
        clcer: function(e) {
                var index = e.currentTarget.dataset.index
                var tids = wx.getStorageSync('set'); //读取
                var lisi = tids.split("#");
                for (var i = 0; i < lisi.length; i++) {
                        if (lisi[i] == "" || typeof(lisi[i]) == "undefined") {
                                lisi.splice(i, 1);
                                i = i - 1;
                        }
                }
                lisi.splice(index, 1);
                var newlisi = ''
                for (var j = 0; j < lisi.length; j++) {
                        newlisi += lisi[j] + '#'
                }
                wx.setStorageSync('set', newlisi)
                this.setData({
                        historylist: lisi
                })
        },
        // 储存
        Storage: function() {
                var that = this
                var text = that.data.searchtext
                var tids = wx.getStorageSync('set'); //读取
                var searchlist = "";
                var k = 0;
                try {
                        var list = tids.split("#");
                        for (var i = 0; i < list.length; i++) {
                                if (list[i].length != 0) {
                                        var c_id = list[i];
                                        if (c_id == text) {
                                                k = 1
                                        }
                                        searchlist += c_id + "#";
                                }
                        }
                } catch (e) {

                }

                if (k == 0) {
                        searchlist += text + "#";
                }
                wx.setStorageSync('set', searchlist) //存
        },
        // 点击热门搜索
        hotlisi: function (e) {
                var that = this
                that.setData({
                        searchtext: e.currentTarget.dataset.text,
                        list:[]
                })
                that.request()
                that.Storage()
        },
        // 点击历史搜索
        cliklisi: function (e) {
                var that = this
                that.setData({
                        searchtext: e.currentTarget.dataset.text,
                        list:[]
                })
                that.request()
        },
        //  读取储存
        read: function() {
                var that = this
                var tids = wx.getStorageSync('set'); //读取
                var lisi = tids.split("#");
                for (var i = 0; i < lisi.length; i++) {
                        if (lisi[i] == "" || typeof(lisi[i]) == "undefined") {
                                lisi.splice(i, 1);
                                i = i - 1;
                        }
                }
                that.setData({
                        historylist: lisi
                })
        },
        // 加载热门搜索词 
        hotreq: function() {
                var that = this
                var header={
                        'authorization': wx.getStorageSync("tekon"),
                        'content-type': "application/x-www-form-urlencoded"
                }
                util.formhttp(app.globalData.appurl +"/qinqiang/search/keywordList","GET", '', header, that.hotci)
        },
        hotci:function(res){
                if (res.status == 401) {
                        this.login()
                }
                console.log(res)
                this.setData({
                        hotlist: res.data
                })
        },
        login: function () {
                var that = this
                wx.login({
                        success: function (res) {
                                var code = res.code
                                var data = {
                                        userCode: code,
                                        userImageUrl: "",
                                        userName: ""
                                }
                                util.promotion(app.globalData.appurl + "/qinqiang/user", "POST", data, that.succeed)
                        }
                })
        },
        succeed: function (res) {
                wx.setStorageSync('tekon', res.data.token)
                wx.setStorageSync('userInfo', res.data.user)
                this.request()

        },
        onReachBottom: function() {
                var that = this
                var pages = that.data.pages
                if (that.data.isopen=="1"){
                        pages++
                        that.setData({
                                pages: pages
                        })
                        that.request()
                }
        },
        request: function() {
                var that = this
                var text = that.data.searchtext
                var pages = that.data.pages
                wx.showLoading({
                        title: '加载中...',
                })
                var data={
                        pageIndex: pages,
                        pageSize: 10,
                        keyword: text
                }
                var header = {
                        'authorization': wx.getStorageSync("tekon"),
                        'content-type': "application/x-www-form-urlencoded"
                }
                util.formhttp(app.globalData.appurl +"/qinqiang/search", "POST", data, header, that.search)
        },
        search:function(res){
                if (res.status == 401) {
                        this.login()
                }
                wx.hideLoading()
                var that=this
                var pages = that.data.pages;
                var list = that.data.list;
                if (pages == 1) {
                        if (res.data.length == 0) {
                                that.setData({
                                        show2: true,
                                        isopen: 1
                                })
                        } else {
                                that.setData({
                                        show1: true,
                                        isopen: 1
                                })
                        }
                }
                if (pages > 1) {
                        if (res.data.length == 0) {
                                that.setData({
                                        show1: true,
                                        isopen: 1
                                })
                        }
                }
                var list = [];
                var totalList = [];
                for (var ids in res.data) {
                        var detail = res.data[ids];
                        var temp = {
                                id: detail.id,
                                title: detail.title,
                                imgUrl: detail.imgUrl,
                                hits: detail.hits,
                                duration: time.formatTime(new Date(detail.duration)),
                                authorimg: detail.userImgUrl,
                                authorname: detail.userName,
                                types: detail.sourceType
                        }
                        list.push(temp)
                }
                totalList = this.data.list.concat(list);
                console.log(totalList)
                that.setData({
                        list: totalList,
                        isopen: 1
                })
        }
})